<?php

namespace app\controller\admin;


use app\common\model\Admin;
use app\common\model\LotteryActivity;
use app\common\service\LotteryActivityService;
use app\common\util\Result;
use think\annotation\route\Group;
use think\annotation\route\Route;

#[Group('admin/lottery_activity')]
class LotteryActivityController extends AdminAuthorizedController
{
    //权限前缀
    protected string $power_prefix = "LotteryActivity";

    /**
     * 获取数据列表
     * @return Result
     */
    #[Route('GET|POST', 'init')]
    public function initList(): Result
    {
        $result = [
            "valid_list" => LotteryActivity::getValidList(),
            "type_list" => LotteryActivity::getTypeList()
        ];
        return Result::success($result);
    }

    /**
     * 后台分页
     * @param Admin $admin
     * @return Result
     */
    #[Route('GET|POST', 'page')]
    public function page(Admin $admin): Result
    {
        $this->checkPower($admin, $this->power_prefix . '_' . 'index');
        $param = request()->param();
        return LotteryActivityService::instance($param)->page($param);
    }


    /**
     * 更新
     * @param Admin $admin
     * @return Result
     * @throws \app\common\exception\CatchException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    #[Route('GET|POST', 'save')]
    public function save(Admin $admin)
    {
        $this->checkPower($admin, $this->power_prefix . '_' . 'edit');
        $param = request()->param(); // 获取请求参数
        $rule = [
            "name|名称" => "require"
        ];
        $this->validate($param, $rule);
        return LotteryActivityService::instance()->save();
    }

    /**
     * 活动详情
     * @param Admin $admin
     * @return Result
     * @throws \app\common\exception\CatchException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    #[Route('GET|POST', 'detail')]
    public function detail(Admin $admin)
    {
        $this->checkPower($admin, $this->power_prefix . '_' . 'detail');
        $param = request()->param(); // 获取请求参数
        $rule = [
            "id" => "require"
        ];
        $this->validate($param, $rule);
        return LotteryActivityService::instance()->detail($param["id"]);
    }

    /**
     * 删除活动
     * @param Admin $admin
     * @return Result
     * @throws \app\common\exception\CatchException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    #[Route('GET|POST', 'delete')]
    public function delete(Admin $admin)
    {
        $this->checkPower($admin, $this->power_prefix . '_' . 'detail');
        $param = request()->param(); // 获取请求参数
        $rule = [
            "id" => "require"
        ];
        $this->validate($param, $rule);
        return LotteryActivityService::instance()->delete($param["id"]);
    }

}